package jnpf;

import jnpf.controller.ExternalController;
import jnpf.model.external.LoginRagReq;
import jnpf.base.ActionResult;
import jnpf.model.external.LoginReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class PlatformAutoLoginRunner implements CommandLineRunner {

    @Autowired
    private Environment environment;

    private final ExternalController externalController;

    public PlatformAutoLoginRunner(ExternalController externalController) {
        this.externalController = externalController;
    }

    @Override
    public void run(String... args) {
        String property = environment.getProperty("ragflow.load_refresh_token", "0");
        try {
            // 登录Dify平台(platform=1)
            if (property.equals("1")) {
                LoginReq difyLoginReq = new LoginReq();
                difyLoginReq.setPlatform("1");
                ActionResult difyResult = externalController.login(difyLoginReq);
                log.error("Dify平台登录结果: {}", difyResult.getMsg());
            }
        } catch (Exception e) {
            log.error("Dify平台自动登录失败", e);
        }

        try {
            // 登录RAG平台(platform=2)
            if (property.equals("1")) {
                LoginReq ragLoginReq = new LoginReq();
                ragLoginReq.setPlatform("2");
                ActionResult ragResult = externalController.login(ragLoginReq);
                log.error("RAG平台登录结果: {}", ragResult.getMsg());
            }
        } catch (Exception e) {
            log.error("RAG平台自动登录失败", e);
        }
    }
}
